package com.bjesc_b2c.app.appraiset.mvp.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.RequestAndResultCode;
import com.bjesc_b2c.app.appraiset.base.BaseActivity;
import com.bjesc_b2c.app.appraiset.mvp.presenter.CustomerPresenter;
import com.bjesc_b2c.app.appraiset.mvp.presenter.impl.CustomerPresenterImpl;
import com.bjesc_b2c.app.appraiset.mvp.view.CustomerView;
import com.bjesc_b2c.app.appraiset.utils.CustomSharedPreferences;
import com.orhanobut.logger.Logger;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import master_bean.CustomerInfoBean;
import rxretrofit_base.model.KeyBean;

import static com.bjesc_b2c.app.appraiset.R.id.edit_loan_remarks;
import static com.bjesc_b2c.app.appraiset.R.id.rlty_car_price;

/**
 * Created by yidong on 2017/5/23.
 */

public class CustomerInfoActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener,CustomerView {
    @InjectView(R.id.tv_client_name)
    TextView clientNameTv;
    @InjectView(R.id.tv_client_grade)
    TextView clientGradeTv;
    @InjectView(R.id.tv_title)
    TextView title;
    @InjectView(R.id.tv_car_on_city)
    TextView cityTv;
    @InjectView(R.id.tv_car_price)
    TextView carPriceTv;
    @InjectView(R.id.tv_client_demand)
    TextView brandNameTv;
    @InjectView(R.id.tv_client_order)
    TextView orderTypeTv;
    @InjectView(edit_loan_remarks)//贷款情况的备注
    EditText loanRemarksEdit;
    @InjectView(R.id.edit_remarks)
    EditText remarksEdit;
    @InjectView(R.id.rg_client_sex)
    RadioGroup radioSex;
    @InjectView(R.id.rg_buycar_time)
    RadioGroup radioBuyCar;
    @InjectView(R.id.rg_client_loan)
    RadioGroup radioLoan;
    @InjectView(R.id.relative_loan_remark)
    RelativeLayout loanRemarkRela;

    private CustomerInfoBean infoBean;
    private String cityName;
    private String brandId;
    private String brandName;
    private String carPriceType;
    private String name;
    private String sex;
    private String tour_level;
    private String installment;
    private String installment_desc;
    private String time;
    private String license_city;
    private String dispid;//派单的id
    private String status;// 2已完成 3见面未成交 4未见面
    private String content;//派单终止的备注信息
    private String seriesId;
    private String seriesName;


    private CustomerPresenter presenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_client_info);
        ButterKnife.inject(this);
        title.setText("客户基本信息");
        presenter = new CustomerPresenterImpl(this);
        String oid = CustomSharedPreferences.getString(this, KeyBean.ORDER_ID.valueOf(),KeyBean.FILE_NAME.valueOf());
        presenter.requestCustomer(oid);
        radioSex.setOnCheckedChangeListener(this);
        radioBuyCar.setOnCheckedChangeListener(this);
        radioLoan.setOnCheckedChangeListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    /**
     * 设置单选按钮
     *
     * @param text
     * @param group
     */
    private void setRadioButtonSelect(String text, RadioGroup group) {
        if (text.equals("1")) {
            ((RadioButton) group.getChildAt(0)).setChecked(true);
        } else if (text.equals("2")) {
            ((RadioButton) group.getChildAt(1)).setChecked(true);
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        switch (checkedId){
            case R.id.rbtn_client_mr:
                sex = "1";
                break;
            case R.id.rbtn_client_ms:
                sex = "2";
                break;
            case R.id.rbtn_loan_yes:
                installment = "1";
                loanRemarkRela.setVisibility(View.VISIBLE);
                break;
            case R.id.rbtn_loan_no:
                installment = "2";
                loanRemarkRela.setVisibility(View.GONE);
                break;
            case R.id.rbtn_buycar_yes:
                time = "1";
                break;
            case R.id.rbtn_buycar_no:
                time = "6";
                break;
        }
    }

    @Override
    public void getCustimerInfo(CustomerInfoBean infoBean) {
        setInfo(infoBean);
    }
    private void setInfo(CustomerInfoBean nfoBean){
        if(nfoBean == null){
            return;
        }else {
            if (!TextUtils.isEmpty(nfoBean.getName())){
                clientNameTv.setText(nfoBean.getName());
            }
            if (!TextUtils.isEmpty(nfoBean.getSex())){
                setRadioButtonSelect(nfoBean.getSex(),radioSex);
            }
            if (!TextUtils.isEmpty(nfoBean.getInstallment())){
                setRadioButtonSelect(nfoBean.getInstallment(),radioLoan);
                if (!TextUtils.isEmpty(nfoBean.getInstallment_desc()) && nfoBean.getInstallment().equals("1")){
                    loanRemarksEdit.setText(nfoBean.getInstallment_desc());
                    loanRemarkRela.setVisibility(View.VISIBLE);
                }
            }
            if (!TextUtils.isEmpty(nfoBean.getTime())){
                if (nfoBean.getTime().equals("6")){
                    setRadioButtonSelect("2",radioBuyCar);
                }else{
                    setRadioButtonSelect("1",radioBuyCar);
                }
            }
            if (!TextUtils.isEmpty(nfoBean.getTour_level())){
                if (nfoBean.getTour_level().equals("1")){
                    clientGradeTv.setText("A");
                }else if(nfoBean.getTour_level().equals("2")){
                    clientGradeTv.setText("B");
                }else if(nfoBean.getTour_level().equals("3")){
                    clientGradeTv.setText("C");
                }
            }
            if (!TextUtils.isEmpty(nfoBean.getDispatch().getStatus())){
//                2博精成交 3战败 4未见面 5见面未成交
                if (nfoBean.getDispatch().getStatus().equals("2")){
                     orderTypeTv.setText("博精成交 ");
                }else if(nfoBean.getDispatch().getStatus().equals("3")){
                    orderTypeTv.setText("战败");
                }else if(nfoBean.getDispatch().getStatus().equals("4")){
                    orderTypeTv.setText("未见面");
                }else if(nfoBean.getDispatch().getStatus().equals("5")){
                    orderTypeTv.setText("见面未成交");
                }
            }
            if (!TextUtils.isEmpty(nfoBean.getDispatch().getContent())){
                remarksEdit.setText(nfoBean.getDispatch().getContent());
            }

            if (!TextUtils.isEmpty(nfoBean.getLicense_city())){
                cityTv.setText(nfoBean.getLicense_city());
            }
            if (!TextUtils.isEmpty(nfoBean.getBrand())){
                brandNameTv.setText(nfoBean.getBrand());
            }
            if (!TextUtils.isEmpty(nfoBean.getPrice_b())){
                carPriceTv.setText(nfoBean.getPrice_b());
            }
            if (!TextUtils.isEmpty(nfoBean.getDispatch().getId())){
                dispid = nfoBean.getDispatch().getId();
            }
        }
    }
    @OnClick({R.id.iv_return,R.id.btn_post,R.id.rlty_car_city,rlty_car_price,R.id.rlty_client_demand,R.id.rlty_order_info,})
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_return:
                finish();
                break;
            case R.id.rlty_car_city:
                Intent intent = new Intent(CustomerInfoActivity.this,CountryVariousCityActivity.class);
                intent.putExtra("cityType",2);
                startIntent(intent,1);
                break;
            case R.id.rlty_car_price:
                Intent intents = new Intent(CustomerInfoActivity.this,SelectTypeActivity.class);
                startIntent(intents,2);
                break;
            case R.id.rlty_client_demand://选择品牌
                Intent intentB = new Intent(CustomerInfoActivity.this, SelectCarBrandActivity.class);
                intentB.putExtra("Brand",2);
                startIntent(intentB,RequestAndResultCode.BasicActivityToSelectCarBrandActivity);
                break;
            case R.id.rlty_order_info:
                Intent intento = new Intent(CustomerInfoActivity.this,SelectTypeActivity.class);
                intento.putExtra("SelecType",2);
                startIntent(intento,3);
                break;
            case R.id.btn_post://提交
                presenter.modifyCustimerInfo(CustomerInfoActivity.this,dispid,modifyInfo());
                break;
        }

    }
     private CustomerInfoBean modifyInfo(){
         if (infoBean == null){
             infoBean = new CustomerInfoBean();
         }
         installment_desc= loanRemarksEdit.getText().toString();
         Logger.d("installment_desc"+installment_desc+ installment);
         content = remarksEdit.getText().toString();
         infoBean.setLicense_city(license_city);
         if (!TextUtils.isEmpty(brandId)) {
             infoBean.setBrand(brandId);
         }
         infoBean.setPrice_b(carPriceType);
         infoBean.setSex(sex);
         infoBean.setInstallment(installment);
         infoBean.setInstallment_desc(installment_desc);
         infoBean.setTime(time);
         CustomerInfoBean.DispatchBean dispatchBean = new CustomerInfoBean.DispatchBean();
         dispatchBean.setContent(content);
         infoBean.setDispatch(dispatchBean);

         return infoBean;
     }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
         switch (resultCode){
             case RequestAndResultCode.CountryVariousCityActivity_ResultCode_Area:
//                 data.getStringExtra("provinceId");
//                 data.getStringExtra("provinceName");
//                 data.getStringExtra("cityId");
                 cityName = data.getStringExtra("cityName");
                 license_city = data.getStringExtra("cityId");
                 cityTv.setText(cityName);
                 break;
             case RequestAndResultCode.SelectCarBrandActivity_ResultCode:
                 brandId = data.getStringExtra("brandId");
                 brandName = data.getStringExtra("brandName");
                 seriesId = data.getStringExtra("seriesId");
                 if (!TextUtils.isEmpty(seriesId)){
                     brandId = brandId+ "-" + seriesId;//品牌加车系
                 }
                 seriesName = data.getStringExtra("seriesName");
                 brandNameTv.setText(brandName+ " "+seriesName);
                 break;
             case RequestAndResultCode.BIGCARSOURCEPHOTOACTIVITY_BACK_CARENGINEFRAGMENT_RESULT://价格
                 carPriceTv.setText(data.getStringExtra("typeName"));
                 carPriceType = data.getStringExtra("typeId");
                 break;
//             case RequestAndResultCode.BIGCARSOURCEPHOTOACTIVITY_BACK_CARENGINEFRAGMENT_RESULT:
//                 orderTypeTv.setText(data.getStringExtra("typeName"));
//                 status = data.getStringExtra("typeId");
//                 break;

         }
    }

}
